package com.whately.core.base.verifycode.util;

import com.whately.core.base.exception.ServiceError;
import com.whately.core.base.exception.ServiceException;
import com.whately.core.base.utils.PwdUtil;
import com.whately.core.base.verifycode.WhatelyVerifyCodeFacade;
import com.whately.core.base.constant.VerifyCodeTypeEnum;
import com.whately.core.base.vo.req.CheckAccountPwdCorrectReqVo;
import com.whately.core.base.vo.req.CheckSmsCodeCorrectReqVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class BaseLoginCheckHelper {


    @Autowired
    private WhatelyVerifyCodeFacade whatelyVerifyCodeFacade;


    /*************************************
     *     获取风控的结果
     ***************************************/
    public boolean getRiskCheckResult(Integer type, String riskUniqueId ){
        VerifyCodeTypeEnum verifyCodeType = VerifyCodeTypeEnum.getVerifyCodeType(type);
        if(verifyCodeType==null|| StringUtils.isBlank(riskUniqueId)){
            log.error("riskCheck verifyCodeType or riskUniqueId is blank");
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR);
        }

        boolean checkPass = whatelyVerifyCodeFacade.riskCheckPass(riskUniqueId);
        return checkPass;

        /*
        //构造返回值
        WhatelySmsSendResultRespVO model = new WhatelySmsSendResultRespVO();

        //风控通过，返回一个可以用的token

        if(checkPass){
            VerifyCodeTypeEnum verifyCodeType4Service = VerifyCodeTypeEnum.risk_check;
            VerifyImgModel captchaModel = null;
            if(VerifyCodeTypeEnum.pfuser_pwd_login.equals(verifyCodeType)){
                captchaModel = whatelyVerifyCodeUtil.getSliderImg(verifyCodeType4Service,riskUniqueId);
            }else{
                captchaModel = whatelyVerifyCodeUtil.getImgVerifyCode(verifyCodeType4Service,riskUniqueId);
            }
            String value = captchaModel.getCaptchaValue();
            String token = captchaModel.getToken();

            model.setToken(token);
            model.setValue(value);
        }
         model.setCheckPass(checkPass);



        return model;

         */
    }

    /**
     * 通过验证码是否有效
     */
    public void checkSmsCodeWhenThroughMobile(CheckSmsCodeCorrectReqVo checkSmsCodeCorrectReqVo, VerifyCodeTypeEnum verifyCodeTypeEnum){
        whatelyVerifyCodeFacade.checkSmsCodeWhenThroughMobile(checkSmsCodeCorrectReqVo,verifyCodeTypeEnum);
    }



    /***********************************************************************************
     * 通过密码和账号登录时，校验工作
     *********************************************************************************/
    public boolean checkWhenLoginByAccountPwd(CheckAccountPwdCorrectReqVo requestVo, VerifyCodeTypeEnum verifyCodeType){
        boolean checkPass;

        if (StringUtils.isNotBlank(requestVo.getToken())) {
            //token 存在，则校验token
            checkPass = this.checkWhenLoginByAccountPwdAndVerifyCode(requestVo, verifyCodeType);
        } else {
            //token 为空，则此处不需要校验token，只是校验用户名和密码格式
            checkPass = this.checkWhenLoginByAccountPwdWithRiskCtrl(requestVo,  verifyCodeType);
        }
        return checkPass;
    }

    /**
     * 带验证的校验
     */
    private boolean checkWhenLoginByAccountPwdAndVerifyCode(CheckAccountPwdCorrectReqVo requestVo, VerifyCodeTypeEnum targetVerifyCodeTypeEnum){
        checkFormatOfAccountPwd(requestVo);

        //检验图形验证码
        String accountNo = requestVo.getAccountNo();
        String token = requestVo.getToken();
        String imgVerifyCode = requestVo.getImgVerifyCode();
        if (StringUtils.isBlank(token) || token.length() <= 16) {
            log.error("BaseLoginHelper checkWhenLoginByAccountPwd verifyCodeType token error ,imgVerifyCode {},token {}", imgVerifyCode, token);
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(),"token格式错误");
        }

        if (StringUtils.isBlank(imgVerifyCode)) {
            log.error("BaseLoginHelper checkWhenLoginByAccountPwd verifyCodeType token error ,imgVerifyCode {},token {}", imgVerifyCode, token);
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(),"验证码不能为空");
        }


        //检验图形验证码是否正确
        whatelyVerifyCodeFacade.checkImgVerifyCodeFromAesToken( targetVerifyCodeTypeEnum, token, accountNo, imgVerifyCode);
        return true;
    }

    /**
     * 没有验证码，风控校验
     */
    private boolean checkWhenLoginByAccountPwdWithRiskCtrl(CheckAccountPwdCorrectReqVo requestVo, VerifyCodeTypeEnum targetVerifyCodeTypeEnum){
        checkFormatOfAccountPwd(requestVo);
        String accountNo = requestVo.getAccountNo();
        //风控校验通过，则返回数据，登录成功！否则告诉前端弹出验证码
        boolean checkPass = this.getRiskCheckResult(targetVerifyCodeTypeEnum.getValue(), accountNo);
        return checkPass;
    }

    /**
     * 检验格式，不正确抛出异常
     */
    private void checkFormatOfAccountPwd(CheckAccountPwdCorrectReqVo requestVo){
        String accountNo = requestVo.getAccountNo();
        String pwd = requestVo.getPwd();

        if (StringUtils.isBlank(accountNo) || StringUtils.isBlank(pwd)) {
            log.error("BaseLoginHelper checkWhenLoginByAccountPwd bad request parameter requestVo {}", requestVo);
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(),"账号密码不能为空");
        }

        //校验密码格式，不正确抛出异常
        PwdUtil.checkPwd(pwd);

    }
}
